<!--
 * @Author: your name
 * @Date: 2022-03-15 14:10:54
 * @LastEditTime: 2022-03-17 15:09:15
 * @LastEditors: Please set LastEditors
 * @Description: 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 * @FilePath: \geyao\geyao\手写call220315.html
-->
<!DOCTYPE html>
<html lang="en">
	<head>
		<meta charset="UTF-8" />
		<meta http-equiv="X-UA-Compatible" content="IE=edge" />
		<meta name="viewport" content="width=device-width, initial-scale=1.0" />
		<title>手写call</title>
	</head>
	<body>
		<script>
			Function.prototype.myCall = function () {
				//如果是undefined和null 则指向window  不是对象用object转为对象
				let params = Array.from(arguments) //得到所以实参数组
				let _obj = params.splice(0, 1)[0] //获取第一位作为对象，即this指向
				//Symbol()表示独一无二的值

				_obj.fn = this

				var result = obj.fn(...params)
				//删除属性
				delete obj.fn

				return result
			}

			var name = 'fangfang'
			var obj = {
				name: 'geyao',
			}

			function fn(a, b, c) {
				console.log(a + b + c + this.name)
				return a + b + c + this.name
			}
			fn.myCall(obj, '我的', '老婆', '是') // 我的老婆是geyao
		</script>
	</body>
</html>
